<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<!--导入easyui样式 -->
<link rel="stylesheet"
	href="${pageContext.request.contextPath}/themes/bootstrap/easyui.css" />
<link rel="stylesheet"
	href="${pageContext.request.contextPath}/themes/icon.css" />
<script type="text/javascript"
	src="${pageContext.request.contextPath}/js/jquery-1.9.1.js"></script>
<script type="text/javascript"
	src="${pageContext.request.contextPath}/js/jquery.easyui.min.js"></script>
<script type="text/javascript"
	src="${pageContext.request.contextPath}/js/easyui-lang-zh_CN.js"></script>

<style type="text/css">

</style>
</head>
<script type="text/javascript">
$(function(){
	$('#dg').datagrid({
		url:'jscx',
		columns:[[  
				{field:'ck',checkbox:true,width:100,align:'center'},       
			    {field:'tid',title:'教师编号',width:100,align:'center'}, 
			    {field:'tname',title:'姓名',width:100,align:'center'},    
			    {field:'tage',title:'年龄',width:100,align:'center'},
			    {field:'tsex',title:'性别',width:100,align:'center'},
			    {field:'address',title:'家庭住址',width:100,align:'center'},
			    {field:'tpnumber',title:'联系电话',width:100,align:'center'},
		]],
		pageSize:5,
	    pagePosition:'bottom',
	    pagination:true,
	    pageList:[5,6,7],
	    rownumbers:true,
	    toolbar: [{
	    	//修改标签
			iconCls: 'icon-edit',
			text:'修改',
			handler: function(){
				//设置下拉列表的高度
				$('#cc1').combobox({    
						panelHeight:50  
					});
				//判断电话号码是否正确
				$("input",$("#tpnumber1").next("span")).blur(function(){
					var spnumber =  $('#tpnumber1').numberbox('getValue');
					if(spnumber<=10000000000||spnumber>=19999999999){
					    	$("#tpm1").show();
					    	$("#save1").linkbutton({ 
					    		disabled:true
					    	})
					}else{
						$("#tpm1").hide();
						$("#save1").linkbutton({ 
				    		disabled:false
				    	})
					}
				    })
				//判断年龄在0——150之间
				$("input",$("#tage1").next("span")).blur(function(){
					var sage = $('#tage1').numberbox('getValue');
					if(sage<=0||sage>=150){
					    	$("#ckage").show();
					    	$("#save1").linkbutton({ 
					    		disabled:true
					    	})
					    	
					}else{
						$("#ckage").hide();
				    	$("#save1").linkbutton({ 
				    		disabled:false
				    	})
					}
				    });
				//获取选取的数据
				var  tea = $('#dg').datagrid('getSelections');
				if(tea.length==1){
					$("#updatetea").dialog("open").dialog('setTitle', '修改教师信息'); 
					//为输入框赋值
					$("#tid1").val(tea[0].tid);
					$("#tname1").val(tea[0].tname);
					var tage11 = tea[0].tage;
					$('#tage1').numberbox('setValue',tage11 );
					$("#address1").val(tea[0].address);
					var tpnumber11 = tea[0].tpnumber;
					$("#tpnumber1").numberbox('setValue',tpnumber11);
					//为性别下拉列表框赋值
					var tsex1 = tea[0].tsex;
					$("#cc1").combo('setText',tsex1);
					$("#save1").linkbutton({
						onClick:function(){
							var tid1 = tea[0].tid;
							var tname1 = $("#tname1").val();
							var tage1 = $("#tage1").numberbox('getValue');
							var tsex1 = $("#cc1").combo('getText');
							var address1 = $("#address1").val();
							var tpnumber1 = $("#tpnumber1").numberbox('getValue');							
							$.ajax({
								url:'updatetea',
								type:'post',
								data:{'tid':tid1,'tname':tname1,'tage':tage1,'tsex':tsex1,'address':address1,'tpnumber':tpnumber1},
								datatype:'text',
								success:function(data){
									if(data=="1"){
										$.messager.alert('success','修改成功！','info');
										$("#updatetea").dialog("close");
										$('#dg').datagrid('reload');
									}else{
										$.messager.alert('error','修改失败！','info');
										$("#updatetea").dialog("close");
										$('#dg').datagrid('reload');
									}
								}
							})
						}
					})
				}else{
					$.messager.alert('error','请只选择一个！','info');	
				}
				//取消按钮
				$("#cancel1").linkbutton({
					onClick:function(){
						$("#tid1").val("");  // 清空修改输入框
						$("#tname1").val("");
						$('#tage1').numberbox('setValue',"");
						$("#address1").val('');
						$("#tpnumber1").numberbox('setValue',"");
						$("#cc1").combo('setText',"");
						$("#ckage").hide();
						$("#tpm1").hide();
						$('#save1').linkbutton('enable');
						$("#updatetea").dialog("close");
						$('#dg').datagrid('uncheckAll');
					}
				})
			}
		},'-',{
			//增加标签
			iconCls: 'icon-add',
			text:'增加',
			handler: function(){
				//判断电话号码是否正确
				$("input",$("#tpnumber").next("span")).blur(function(){
					var spnumber =  $('#tpnumber').numberbox('getValue');
					if(spnumber<=10000000000||spnumber>=19999999999){
					    	$("#tpm").show();
					    	$("#save").linkbutton({ 
					    		disabled:true
					    	})
					}else{
						$("#tpm").hide();
						$("#save").linkbutton({ 
				    		disabled:false
				    	})
					}
				    })
				//判断年龄在0——150之间
				$("input",$("#tage").next("span")).blur(function(){
					var sage = $('#tage').numberbox('getValue');
					if(sage<=0||sage>=150){
					    	$("#cage").show();
					    	$("#save").linkbutton({ 
					    		disabled:true
					    	})
					    	
					}else{
						$("#cage").hide();
				    	$("#save").linkbutton({ 
				    		disabled:false
				    	})
					}
				    });
				//点击跳出增加教师窗口
				$("#inserttea").dialog("open").dialog('setTitle', '增加教师');
				$("#uname").val(""); //使得新增窗口弹出都为空
				$("#upsw").val("");
				$("#tid").val("");
				$("#tname").val("");
				$("#tage").val("");
				$("#tsex").val("");
				$("#address").val("");
				$("#tpnumber").numberbox('setValue',"");
				$('#cc').combobox({    
					panelHeight:50  
				});
				//鼠标移开事件，判断此用户名是否被注册
				$("#uname").blur(function(){
 					 var uname2 = $("#uname").val();
 					 $.ajax({
 						 url:'cxuname',
 						 type:'post',
 						 data:{'uname2':uname2},
 						 datatype:'text',
 						 success:function(data){
 							 if(data=="1"){
 								$("#cuname").show();
 								$('#save').linkbutton('disable');
 							 }else{
 								 $("#cuname").hide();
 								$('#save').linkbutton('enable');
 							 }
 						 } 
 					 })
 					 
 					 
					});
				$("#save").linkbutton({				
					//点击事件，将窗口中的数据使用异步的方式传到controller
					onClick:function(){
						var tid = $("#tid").val();
						var uname = $("#uname").val();
						var upsw = $("#upsw").val();
						var tname = $("#tname").val();
						var tage = $("#tage").val();
						var tsex = $("#cc").combo('getText');
						var address = $("#address").val();
						var tpnumber = $("#tpnumber").numberbox('getValue');						
						$.ajax({
							url:'inserttea',
							type:'post',
							data:{'uname':uname,'upsw':upsw,'tid':tid,'tname':tname,'tage':tage,'tsex':tsex,'address':address,'tpnumber':tpnumber},
							datatype:'text',
							success:function(data){
								if(data=='1'){
									$.messager.alert('success','添加成功！','info');
									$("#uname").val(""); //清空输入框
									$("#upsw").val("");
									$("#tid").val("");
									$("#tname").val("");
									$("#tage").val("");
									$("#tsex").val("");
									$("#address").val("");
									$("#tpnumber").numberbox('setValue',"");
									$("#inserttea").dialog("close");
									$('#dg').datagrid('reload');
								}else{
									$.messager.alert('error','添加失败！','info');
									$("#inserttea").dialog("close");
								}	
							}
						})
					}
				})				
				//取消按钮
				$("#cancel").linkbutton({
					onClick:function(){
						$("#uname").val("");
						$("#upsw").val("");
						$("#tid").val("");
						$("#tname").val("");
						$('#tage').numberbox('setValue',"");
						$("#tsex").val("");
						$("#address").val("");
						$("#tpnumber").numberbox('setValue',"");
						$("#cage").hide();
						$("#cuname").hide();
						$("#tpm").hide();
						$('#save').linkbutton('enable');
						$("#inserttea").dialog("close");
						$('#dg').datagrid('uncheckAll');
					}
				})
			}
		},'-',{
			//删除标签
			iconCls: 'icon-remove',
			text:'删除',
			handler: function(){
				//弹出确定框
				var  teas = $('#dg').datagrid('getSelections');
				if(teas.length>0){
					$("#deletetea").dialog("open").dialog('setTitle', '删除教师');
					$("#yes").linkbutton({
						onClick:function(){
							var tid = [];
							for (var i = 0; i < teas.length; i++) {
								tid.push(teas[i].tid);
							}
							$.ajax({
								url:'deleteteas',
								type:'post',
								data:{'tid':tid},
								datatype:'text',
								success:function(data){
									if(data=='1'){
										$.messager.alert('success','删除成功！','info');
										$("#deletetea").dialog("close");
										$('#dg').datagrid('reload');
									}else{
										$.messager.alert('error','删除失败！','info');
										$("#deletetea").dialog("close");
										$('#dg').datagrid('reload');
									}
								}
							})	
							
						}
					})
				}else{
					$.messager.alert('MSG','请至少选择一个！','info');
				}
				$("#no").linkbutton({
					onClick:function(){
						$('#dg').datagrid('uncheckAll');
						$("#deletetea").dialog("close");
						}
					})
			}
		}]
	});	
});


</script>
<body>
  	 <table id="dg"></table>
  	 <!--修改彈出窗口 -->
  <div id="updatetea" class="easyui-dialog"  closed="true" buttons="#dlg1-buttons" > 
		 <div  style="width: 250px;height: 250px;margin-top: 30px;margin-left: 30px">
             姓&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;名<input id="tname1" name="tname1"  class="easyui-validatebox"  style="width:90px "/><br><br>    
             年&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;龄<input id="tage1" name="tage1" class="easyui-numberbox"  style="width:90px "/>
       <span id="ckage" style="font-size: xx-small;display: none;">年龄在1-150岁之间</span> <br><br>     
             性&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;别<select id="cc1" class="easyui-combobox" name="dept" size=2 style="width:40px;">   	  
												<option>男</option>   
												<option>女</option>     
											</select><br><br>
             家庭住址<input id="address1" name="address1" class="easyui-validatebox" style="width:90px " /><br><br>
             联系电话<input id="tpnumber1" name="tnumber1" class="easyui-numberbox" style="width:90px " />
	<span id="tpm1" style="font-size: xx-small;display: none;">请输入正确的格式</span>
      </div>
       <div id="dlg1-buttons">
        <button id="save1"  class="easyui-linkbutton"  iconcls="icon-save">确认</button>
        <a id="cancel1" class="easyui-linkbutton"  iconcls="icon-cancel">取消</a>
        </div>
        </div> 
  	 <!-- 增加彈出窗口 -->
  <div id="inserttea" class="easyui-dialog"  closed="true" buttons="#dlg2-buttons" > 
		 <div  style="width: 250px;height: 250px;margin-top: 20px;margin-left: 30px">
	<!--  教师编号<input id="tid" name="tid" class="easyui-validatebox"  /><br><br> -->  
              用&nbsp;&nbsp;户&nbsp;&nbsp;名<input id="uname" name="uname"  placeholder="5-11位字母" class="easyui-validatebox"  style="width:90px;font-size:1px "/>
      <span id="cuname" style="font-size: xx-small;display: none;">用户名已存在</span><br><br>                
              密&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;码<input id="upsw" name="upsw" placeholder="6-12位字母或数字" class="easyui-validatebox"  style="width:90px;font-size:1px "/><br><br>      
              姓&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;名<input id="tname" name="tname" class="easyui-validatebox"  style="width:90px "/><br><br>
              年&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;龄<input id="tage" name="tage" class="easyui-numberbox"  style="width:90px "/>          
        <span id="cage" style="font-size: xx-small;display: none;">年龄在1-150岁之间</span> <br><br>      
              性&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;别<select id="cc" class="easyui-combobox" name="dept" style="width:40px;">   	  
				<option selected>男</option>   
				<option>女</option>     
			</select><br><br>       
             家庭住址<input id="address" name="address" class="easyui-validatebox"  style="width:90px "/><br><br>
             联系电话<input id="tpnumber" name="tnumber" class="easyui-numberbox"  style="width:90px "/> 
        <span id="tpm" style="font-size: xx-small;display: none;">请输入正确的格式</span>      
      </div>
       <div id="dlg2-buttons">
        <button id="save"  class="easyui-linkbutton"  iconcls="icon-save">确认</button>
        <button id="cancel" class="easyui-linkbutton" iconcls="icon-cancel">取消</button>
        </div>
        </div>
        <!-- 删除彈出窗口 -->
  		<div id="deletetea" class="easyui-dialog"  closed="true" buttons="#dlg3-buttons" > 
		 <div  style="width: 100px;height: 30px;margin-top: 10px;margin-left: 30px">
	  		<p>确认删除吗？</p>
	  	 </div>
       <div id="dlg3-buttons">
        <button id="yes"  class="easyui-linkbutton"  iconcls="icon-save">确认</button>
        <a id="no" class="easyui-linkbutton"  iconcls="icon-cancel">取消</a>
        </div>
        </div>
</body>
</html>